package cn.iocoder.yudao.module.course.controller.admin.interactive;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.course.controller.admin.interactive.vo.*;
import cn.iocoder.yudao.module.course.dal.dataobject.interactive.InteractiveDO;
import cn.iocoder.yudao.module.course.service.interactive.InteractiveService;

@Tag(name = "管理后台 - 课程交互")
@RestController
@RequestMapping("/course/interactive")
@Validated
public class InteractiveController {

    @Resource
    private InteractiveService interactiveService;

    @PostMapping("/create")
    @Operation(summary = "创建课程交互")
    @PreAuthorize("@ss.hasPermission('course:interactive:create')")
    public CommonResult<Long> createInteractive(@Valid @RequestBody InteractiveSaveReqVO createReqVO) {
        return success(interactiveService.createInteractive(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程交互")
    @PreAuthorize("@ss.hasPermission('course:interactive:update')")
    public CommonResult<Boolean> updateInteractive(@Valid @RequestBody InteractiveSaveReqVO updateReqVO) {
        interactiveService.updateInteractive(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程交互")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('course:interactive:delete')")
    public CommonResult<Boolean> deleteInteractive(@RequestParam("id") Long id) {
        interactiveService.deleteInteractive(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程交互")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('course:interactive:query')")
    public CommonResult<InteractiveRespVO> getInteractive(@RequestParam("id") Long id) {
        InteractiveDO interactive = interactiveService.getInteractive(id);
        return success(BeanUtils.toBean(interactive, InteractiveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程交互分页")
    @PreAuthorize("@ss.hasPermission('course:interactive:query')")
    public CommonResult<PageResult<InteractiveRespVO>> getInteractivePage(@Valid InteractivePageReqVO pageReqVO) {
        PageResult<InteractiveDO> pageResult = interactiveService.getInteractivePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InteractiveRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程交互 Excel")
    @PreAuthorize("@ss.hasPermission('course:interactive:export')")
    @OperateLog(type = EXPORT)
    public void exportInteractiveExcel(@Valid InteractivePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InteractiveDO> list = interactiveService.getInteractivePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程交互.xls", "数据", InteractiveRespVO.class,
                        BeanUtils.toBean(list, InteractiveRespVO.class));
    }

}